package com.cgbl.cg.ui.vip

import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.fastjson.JSON
import com.cgbl.cg.BR
import com.cgbl.cg.R
import com.cgbl.cg.adapter.ConfirmOrderProductAdapter
import com.cgbl.cg.adapter.ShoppingCarAdapter
import com.cgbl.cg.databinding.AppActivityConfirmOrderBinding
import com.cgbl.cg.view.SelectCouponPop
import com.cgbl.cg.view.UpdateJobPop
import com.cgbl.cg.vm.order.ConfirmOrderViewModel
import com.lxj.xpopup.XPopup
import com.robot.base.base.activity.GLBaseActivity
import com.robot.base.configs.AppConstants
import com.robot.base.model.CheckOrderIdsBean
import com.robot.base.model.ConfirmOrderProductBean
import com.robot.base.model.CouponBean
import com.robot.base.model.ShopCartBean

@Route(path = AppConstants.Router.Mine.A_MINE_CONFIRM_ORDER)
class ConfirmOrderActivity :
    GLBaseActivity<AppActivityConfirmOrderBinding, ConfirmOrderViewModel>() {

    lateinit var shoppingCarAdapter: ConfirmOrderProductAdapter

    private lateinit var selectCouponPop: SelectCouponPop

    private var ids = mutableListOf<CheckOrderIdsBean>()

    private var shopCarts = mutableListOf<ConfirmOrderProductBean>()

    override fun initComponents() {
        initTiterBar()
        initView()
        viewModel.initParams(ids)
        viewModel.queryCoupon()
        viewModel.checkOrder()
    }

    override fun initViewObservable() {
        viewModel.uc.showSelectCouponPop.observe(this) {
            XPopup.Builder(this).asCustom(selectCouponPop).show()
        }
        viewModel.uc.refreshCouponEvent.observe(this) {
            selectCouponPop.couponAdapter.setNewInstance(it as MutableList<CouponBean>?)
        }
        viewModel.uc.refreshGoodsEvent.observe(this) {
            shoppingCarAdapter.setNewInstance(it as MutableList<ConfirmOrderProductBean>?)
        }
    }

    override fun initParam() {
        super.initParam()
        val carts = intent.getStringExtra(AppConstants.BundleKey.SHOP_CART_BEAN) ?: ""
        if (!TextUtils.isEmpty(carts)) {
            shopCarts = JSON.parseArray(carts, ConfirmOrderProductBean::class.java)
        }
        shopCarts.forEach {
            ids.add(CheckOrderIdsBean(projectId = it.projectId, projectType = it.projectType))
        }
    }

    private fun initTiterBar() {
        val titleView = findViewById<TextView>(R.id.tv_title)
        val backView = findViewById<ImageView>(R.id.iv_back)
        titleView.text = "确认订单"
        backView.setOnClickListener { finish() }
    }

    private fun initView() {
        selectCouponPop = SelectCouponPop(this)

        selectCouponPop.setOnSelectCouponListener {
            viewModel.setCoupon(it)
        }
        shoppingCarAdapter = ConfirmOrderProductAdapter()
        binding.goodsList.apply {
            layoutManager = LinearLayoutManager(this@ConfirmOrderActivity)
            adapter = shoppingCarAdapter
        }
        shoppingCarAdapter.setNewInstance(shopCarts)
    }

    override fun initContentView(savedInstanceState: Bundle?): Int =
        R.layout.app_activity_confirm_order

    override fun initVariableId(): Int = BR.viewModel
}